// WireClient Program for PCMod (CLIENT)

PROG.PName = "WireClient"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.Icon = "icons/ico_wire"

PROG.NiceName = "Wire Client"

function PROG:BuildWindow( WindowName, Desktop, Object )
	//DInterface:AddLabel( "lblInfo", Desktop, "WireClient V1", 5, 5 )
	
	if (WindowName == "Main") then
		DInterface:AddLabel( "lblInfo2", Desktop, "IP:", 5, 25 )
		DInterface:AddTextbox( "txtIP", Desktop, "192.168.0.1", 25, 25, 100, 20 )
		
		DInterface:AddLabel( "lblInfo3", Desktop, "Set Output", 5, 50 )
		DInterface:AddTextbox( "txtOID", Desktop, "1", 80, 50, 40, 20 )
		DInterface:AddLabel( "lblInfo4", Desktop, "to", 140, 50 )
		DInterface:AddTextbox( "txtOVA", Desktop, "0", 170, 50, 40, 20 )
		DInterface:AddButton( "btnSet", Desktop, "Set", 220, 50, 60, 20, function()
			SendCommand( "send_output",
				DInterface:GetTextboxValue( "txtIP", Desktop ),
				DInterface:GetTextboxValue( "txtOID", Desktop ),
				DInterface:GetTextboxValue( "txtOVA", Desktop ) );
		end )
		
		DInterface:AddTable( "tblInputs", Desktop, 300, 5, Object:GetWide()-310, Object:GetTall()-40 )
		DInterface:TableAddColumn( "tblInputs", Desktop, "Input Name" )
		DInterface:TableAddColumn( "tblInputs", Desktop, "Input Value" )
		DInterface:AddButton( "btnGet", Desktop, "Get Inputs", 300, Object:GetTall()-30, Object:GetWide()-310, 20, function()
			SendCommand( "get_inputs",
				DInterface:GetTextboxValue( "txtIP", Desktop ) );
		end )
	end
	
	SendCommand( "updateme" )
	
	DInterface:AddButton( "btnQuit", Desktop, "X", Object:GetWide()-20, 0, 20, 20, function() SendCommand( "quit" ); end )
end

function PROG:ClearWindow( Desktop )
	DInterface:RemoveLabel( "lblInfo", Desktop )
	DInterface:RemoveLabel( "lblInfo2", Desktop )
	DInterface:RemoveLabel( "lblInfo3", Desktop )
	DInterface:RemoveLabel( "lblInfo4", Desktop )
	
	DInterface:RemoveTextbox( "txtIP", Desktop )
	DInterface:RemoveTextbox( "txtOID", Desktop )
	DInterface:RemoveTextbox( "txtOVA", Desktop )
	
	DInterface:RemoveTable( "tblInputs", Desktop )
	
	DInterface:RemoveButton( "btnSet", Desktop )
	DInterface:RemoveButton( "btnGet", Desktop )
	
	DInterface:RemoveButton( "btnQuit", Desktop )
end

// Data[1] = Tag
// Data[2] = Content
function PROG:DataRecieved( Desktop, Data )
	if (Data[1] == "lastip") then DInterface:SetTextboxText( "txtIP", Desktop, Data[2] ) end
	if (Data[1] == "inputs") then
		local txt = Data[2]
		DInterface:TableClearRows( "tblInputs", Desktop )
		local inputs = string.Explode( "+", txt )
		for i=1,#inputs do
			DInterface:TableAddRow( "tblInputs", Desktop, "> " .. tostring(i) .. " <", tostring( inputs[i] ) )
		end
	end
end

function PROG:Paint( x, y, w, h )
	draw.SimpleText( self.NiceName, "SmallPCText", x + (w*0.05), y + (h*0.05), Color( 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
end